package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.constants.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {

        //参数校验
        if (dto==null || dto.getArticleId()==null &&(dto.getOperation()==0 || dto.getOperation()==1)){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前登录id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
             CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据id查询实体数据
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        Query query = Query.query(Criteria.where("entryId").is(behaviorEntry.getId())
                .and("articleId").is(dto.getArticleId()));
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if ( behaviorEntry!=null) {


            if ( dto.getOperation()==0 && apCollection==null){
               //没有收藏 新增
                apCollection=new ApCollection();
                apCollection.setCollectionTime(new Date());
                apCollection.setEntryId(behaviorEntry.getId());
                apCollection.setType((short)0);
                apCollection.setArticleId(dto.getArticleId());
               //保存收藏
                mongoTemplate.save(apCollection);
            }
            //取消收藏
            if (dto.getOperation()==1){
                mongoTemplate.remove(query, ApCollection.class);
            }
        }

        NewBehaviorDTO mess = new NewBehaviorDTO();
        mess.setType(NewBehaviorDTO.BehaviorType.VIEWS);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("点赞行为 发送消息 ==>{}",JSON.toJSONString(mess));

        return ResponseResult.okResult();
    }
}
